package com.liusand.admin.filter;

import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author LiuSha
 */
@WebFilter("/*")
@Component
@Slf4j
public class LastFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Boolean flag = (Boolean) request.getAttribute("flag");

        if(flag){
            filterChain.doFilter(servletRequest, servletResponse);
        }

        String requestURI = request.getRequestURI();
        if("/admin/login".equals(requestURI)){
            filterChain.doFilter(servletRequest, servletResponse);
        }

//        if("get".equals(request.getMethod().toLowerCase(Locale.ROOT))){
//            filterChain.doFilter(servletRequest, servletResponse);
//        }

//        ServletInputStream servletInputStream = request.getInputStream();
//        InputStreamReader isr = new InputStreamReader(servletInputStream, StandardCharsets.UTF_8);
//        BufferedReader bf = new BufferedReader(isr);
//
//        String s;
//        StringBuilder sb = new StringBuilder();
//        while((s = bf.readLine()) != null){
//            sb.append(s);
//        }
//        HashMap map = JSONObject.parseObject(sb.toString(), HashMap.class);
//        if(map != null){
//            String token = map.get("token").toString();
//            boolean verify = JWTUtil.verify(token, "ASDFGHJKL".getBytes());
//            if(verify){
//                filterChain.doFilter(servletRequest, servletResponse);
//            }
//        }

    }
}
